package vashion.azeroth.core.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import vashion.azeroth.contant.AzerothConstants;
import vashion.azeroth.contant.FavoriteErrorEnum;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.contant.MemberErrorEnum;
import vashion.azeroth.context.pojo.PersonalFavoritesItemContext;
import vashion.azeroth.core.item.pojo.ItemDO;
import vashion.azeroth.core.item.query.ItemQueryDO;
import vashion.azeroth.core.manager.item.ItemReadManger;
import vashion.azeroth.core.manager.personal.PersonalFavoritesManager;
import vashion.azeroth.core.manager.personal.PersonalMemberManager;
import vashion.azeroth.core.manager.personal.PersonalPublishFilesManager;
import vashion.azeroth.core.manager.personal.config.ConfigExchangeRateManager;
import vashion.azeroth.core.personal.pojo.PersonalFavorites;
import vashion.azeroth.core.personal.pojo.PersonalFeed;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.personal.pojo.PersonalPublishFiles;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalFavoritesService;
import vashion.azeroth.core.service.PersonalMemberService;
import vashion.azeroth.core.service.PersonalPublishFilesService;
import vashion.azeroth.core.service.item.ItemReadService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PersonalFavoritesServiceImpl<T> implements PersonalFavoritesService<T> {

	private final static Logger log = LoggerFactory.getLogger(PersonalFavoritesServiceImpl.class);

	@Autowired
	PersonalFavoritesManager personalFavoritesManager;

	@Autowired
	PersonalMemberService<?> personalMemberService;

	@Autowired
	PersonalMemberManager personalMemberManager;

	@Autowired
	ItemReadService itemReadService;

	@Autowired
	PersonalPublishFilesService<?> personalPublishFilesService;

	@Autowired
	PersonalPublishFilesManager personalPublishFilesManager;

	@Autowired
	private PersonalMemberManager memberManager;
	
	@Autowired
	private ItemReadManger itemReadManger;

	@Autowired
	private ConfigExchangeRateManager configExchangeRateManager;

	@Override
	public ApiResult<Boolean> addFavorit(PersonalFavorites personalFavorites) {

		ApiResult<Boolean> result = validParam(personalFavorites);

		// 参数不合法，直接返回
		if (!result.isSuccess()) {

			return result;

		}

		try {

			// 默认状态设置为 NORMAL
			personalFavorites.setStatus(AzerothConstants.PersonalFavtories.PersonalFavtoriesStatus.NORMAL);

			// 先查询关联关系
			List<PersonalFavorites> queryResult = (List<PersonalFavorites>) personalFavoritesManager
					.find(personalFavorites);

			// 如果已存在，直接返回
			if (queryResult.size() >= 1 && AzerothConstants.PersonalFavtories.PersonalFavtoriesStatus.NORMAL
					.equals(queryResult.get(0).getStatus())) {

				return ApiResult.newErrorResult(FavoriteErrorEnum.ALREADY_FAVORITE.getErrorCode(),
						FavoriteErrorEnum.ALREADY_FAVORITE.getErrorMessage(), false);
			}

			// 确认商品或者贴子是否存在

			if (personalFavorites.getType().equals(AzerothConstants.PersonalFavtories.PersonalFavtoriesType.ITEM)) {

				Long itemId = Long.valueOf(personalFavorites.getTargetCode());

				ApiResult<ItemDO> api = itemReadService.getItemDetail(itemId, false);

				if (!api.isSuccess() || api.getData() == null) {

					return ApiResult.newErrorResult(FavoriteErrorEnum.ITEM_NOT_EXIST.getErrorCode(),
							FavoriteErrorEnum.ITEM_NOT_EXIST.getErrorMessage(), false);

				}

				ItemDO itemDO = api.getData();

				Long userId = itemDO.getUserId();

				if (!userId.equals(Long.valueOf(personalFavorites.getPublishMemberId()))) {

					return ApiResult.newErrorResult(FavoriteErrorEnum.NOT_BELONG.getErrorCode(),
							FavoriteErrorEnum.NOT_BELONG.getErrorMessage(), false);
				}


			}

			if (personalFavorites.getType().equals(AzerothConstants.PersonalFavtories.PersonalFavtoriesType.POST)) {

				// DO NOTHING
				// TODO

				Integer postId = Integer.valueOf(personalFavorites.getTargetCode());

				PersonalPublishFiles personalPublishFiles = personalPublishFilesManager.get(postId);

				if (personalPublishFiles == null || personalPublishFiles.getPublishFileId() == null) {
					return ApiResult.newErrorResult(FavoriteErrorEnum.POST_NOT_EXIST.getErrorCode(),
							FavoriteErrorEnum.POST_NOT_EXIST.getErrorMessage(), false);

				}

			}

			// 补合参数
			fillParam(personalFavorites);

			// 添加时间参数
			Date d = new Date();
			personalFavorites.setTimeAdd(d);
			personalFavorites.setTimeEdit(d);
			personalFavorites.setStatus(AzerothConstants.PersonalFavtories.PersonalFavtoriesStatus.NORMAL);

			if (queryResult.size() >= 1) {
				int addResult = personalFavoritesManager.update(personalFavorites);
			} else {
				PersonalFavorites addResult = personalFavoritesManager.save(personalFavorites);
			}

			updateFeed(personalFavorites);

			return result;

		} catch (Exception e) {

			log.error(e.getMessage());

			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage(), false);
		}

	}

	@Override
	public ApiResult<Boolean> cancelFavorite(PersonalFavorites personalFavorites) {

		ApiResult<Boolean> result = validParam(personalFavorites);

		// 参数不合法，直接返回
		if (!result.isSuccess()) {

			return result;
		}

		try {

			// 先查询关联关系
			// 默认状态设置为 NORMAL
			personalFavorites.setStatus(AzerothConstants.PersonalFavtories.PersonalFavtoriesStatus.NORMAL);

			List<PersonalFavorites> queryResultList = (List<PersonalFavorites>) personalFavoritesManager
					.find(personalFavorites);

			if (queryResultList.size() == 0) {

				return ApiResult.newErrorResult(FavoriteErrorEnum.NOT_FAVORITE.getErrorCode(),
						FavoriteErrorEnum.NOT_FAVORITE.getErrorMessage(), false);

			}

			/**
			 * 
			 * 取消收藏
			 * 
			 * 默认只有一条，拿第一条数据
			 */
			PersonalFavorites queryResult = queryResultList.get(0);

			queryResult.setStatus(AzerothConstants.PersonalFavtories.PersonalFavtoriesStatus.DELETE);

			queryResult.setTimeEdit(new Date());

			int cnt = personalFavoritesManager.updateById(queryResult);

			if (cnt > 0) {

				updateFeed(personalFavorites);

				return ApiResult.newSuccessResult(true);

			} else {

				return ApiResult.newSuccessResult(false);

			}
		} catch (Exception e) {

			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage(), false);
		}

	}

	@Override
	public ApiResult<List<PersonalFavoritesItemContext>> queryMyFavoriteItem(Long memberId, int pageNum, int pageSize,
			String targetCurno) {

		if (memberId == null || memberId <= 0) {

			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
					"会员ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}

		// 构造参数
		PersonalFavorites pf = new PersonalFavorites();

		pf.setMemberId(memberId);

		pf.setFirstRow(pageNum);

		pf.setPageSize(pageSize);

		pf.setType(AzerothConstants.PersonalFavtories.PersonalFavtoriesType.ITEM);

		pf.setStatus(AzerothConstants.PersonalFavtories.PersonalFavtoriesStatus.NORMAL);

		try {

			int totalCount = personalFavoritesManager.findCnt(pf);

			List<PersonalFavorites> data = personalFavoritesManager.find(pf);

			List<PersonalFavoritesItemContext> list = convertPersonalFavoritesItemContext(data);

			// 检查用户是供应商还是代理商
			PersonalMember member = memberManager.get(memberId);
			if (null == member) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(),
						GenericErrorEnum.NOT_EXIST.getErrorMessage());
			}
			if (AzerothConstants.PersonalMember.MemberType.agent == member.getType()
					&& !CollectionUtils.isEmpty(list)) {
				for (PersonalFavoritesItemContext personalFavoritesItemContext : list) {
					// 抹平差价
					if (member.getInviteMemberId().longValue() != personalFavoritesItemContext.getPublishMemberId().longValue()) {
						ItemDO itemDO = new ItemDO();
						itemDO.setItemCode(personalFavoritesItemContext.getItemCode());
						itemDO.setPrice(Long.valueOf(personalFavoritesItemContext.getItemPrice()));
						personalFavoritesItemContext.setItemPrice(
								String.valueOf(itemReadManger.getHigherSupplierPrice(member, itemDO)));
					}
					//转换货币
					exchangePrice(personalFavoritesItemContext, targetCurno);
				}
			} else if (AzerothConstants.PersonalMember.MemberType.agent != member.getType()) {
				return ApiResult.newErrorResult(MemberErrorEnum.NOT_EXISTID.getErrorCode(),
						MemberErrorEnum.NOT_EXISTID.getErrorMessage());
			}

			return ApiResult.newSuccessResult(list, totalCount);

		} catch (Exception e) {

			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());

		}

	}

	@Override
	public ApiResult<List<PersonalFeed>> queryMyFacoritePost(Long memberId, int pageNo, int pageSize) {

		if (memberId == null || memberId <= 0) {

			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
					"会员ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());

		}

		// 构造参数
		PersonalFavorites pf = new PersonalFavorites();

		pf.setMemberId(memberId);

		pf.setFirstRow(pageNo);

		pf.setPageSize(pageSize);

		pf.setType(AzerothConstants.PersonalFavtories.PersonalFavtoriesType.POST);

		pf.setStatus(AzerothConstants.PersonalFavtories.PersonalFavtoriesStatus.NORMAL);

		try {

			int totalCount = personalFavoritesManager.findCnt(pf);

			List<PersonalFavorites> data = personalFavoritesManager.find(pf);

			List<PersonalFeed> feedList = convertPersonalFeedList(data, memberId);

			return ApiResult.newSuccessResult(feedList, totalCount);

		} catch (Exception e) {

			log.error(e.getMessage());

			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());

		}

	}

	@Override
	public ApiResult<Boolean> queryIsFavorited(Long memberId, Integer type, String targetCode) {

		if (memberId == null || memberId <= 0) {

			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
					"会员ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());

		}

		if (null == type) {

			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
					"类型" + GenericErrorEnum.NOT_EMPTY.getErrorMessage(), false);
		}

		if (!AzerothConstants.PersonalFavtories.PersonalFavtoriesType.ITEM.equals(type)
				&& !AzerothConstants.PersonalFavtories.PersonalFavtoriesType.POST.equals(type)) {

			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(),
					"类型" + GenericErrorEnum.NOT_EXIST.getErrorMessage(), false);
		}

		if (StringUtils.isEmpty(targetCode)) {

			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
					"商品/贴子编码" + GenericErrorEnum.NOT_EMPTY.getErrorMessage(), false);
		}

		// 构造参数
		PersonalFavorites pf = new PersonalFavorites();

		pf.setMemberId(memberId);

		pf.setType(type);

		pf.setTargetCode(targetCode);

		pf.setStatus(1);

		try {

			List<PersonalFavorites> data = personalFavoritesManager.find(pf);

			if (data.size() > 0) {

				return ApiResult.newSuccessResult(true);
			} else {

				return ApiResult.newSuccessResult(false);

			}

		} catch (Exception e) {

			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());

		}

	}

	public ApiResult<Integer> queryFavoriteCnt(Long memberId, Integer type, String targetCode) {

		if (null == type) {

			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
					"类型" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}

		if (!AzerothConstants.PersonalFavtories.PersonalFavtoriesType.ITEM.equals(type)
				&& !AzerothConstants.PersonalFavtories.PersonalFavtoriesType.POST.equals(type)) {

			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(),
					"类型" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}

		// 构造参数
		PersonalFavorites pf = new PersonalFavorites();

		if (null != memberId) {
			pf.setMemberId(memberId);
		}

		pf.setType(type);

		if (!StringUtils.isEmpty(targetCode)) {
			pf.setTargetCode(targetCode);
		}

		pf.setStatus(AzerothConstants.PersonalFavtories.PersonalFavtoriesStatus.NORMAL);

		try {

			int cnt = personalFavoritesManager.findCnt(pf);

			return ApiResult.newSuccessResult(cnt);

		} catch (Exception e) {

			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());

		}

	}

	// 校验参合法性，不合法返FALSE
	private ApiResult<Boolean> validParam(PersonalFavorites personalFavorites) {

		if (null == personalFavorites.getMemberId()) {

			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
					"会员ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage(), false);
		}

		// if (StringUtils.isEmpty(personalFavorites.getMemberName())) {
		//
		// return ApiResult.newErrorResult(
		// GenericErrorEnum.NOT_EMPTY.getErrorCode(), "会员名称"
		// + GenericErrorEnum.NOT_EMPTY.getErrorMessage(),
		// false);
		// }

		if (null == personalFavorites.getPublishMemberId()) {

			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
					"发布者ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage(), false);

		}

		// if (StringUtils.isEmpty(personalFavorites.getPublishMemberName())) {
		//
		// return ApiResult.newErrorResult(
		// GenericErrorEnum.NOT_EMPTY.getErrorCode(), "发布者名称"
		// + GenericErrorEnum.NOT_EMPTY.getErrorMessage(),
		// false);
		// }

		if (StringUtils.isEmpty(personalFavorites.getTargetCode())) {

			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
					"商品/贴子编码" + GenericErrorEnum.NOT_EMPTY.getErrorMessage(), false);
		}

		if (!isNumeric(personalFavorites.getTargetCode())) {

			return ApiResult.newErrorResult(GenericErrorEnum.PARAMETER_ERROR.getErrorCode(),
					"targetCode格式" + GenericErrorEnum.PARAMETER_ERROR.getErrorMessage(), false);

		}

		if (null == personalFavorites.getType()) {

			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
					"类型" + GenericErrorEnum.NOT_EMPTY.getErrorMessage(), false);
		}

		if (!AzerothConstants.PersonalFavtories.PersonalFavtoriesType.ITEM.equals(personalFavorites.getType())
				&& !AzerothConstants.PersonalFavtories.PersonalFavtoriesType.POST.equals(personalFavorites.getType())) {

			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(),
					"类型" + GenericErrorEnum.NOT_EXIST.getErrorMessage(), false);
		}

		// 用户是否存在校验
		Long memberId = personalFavorites.getMemberId();

		String memberName = personalFavorites.getMemberName();

		ApiResult<Boolean> result = checkMemberExist(memberId, memberName);

		if (!result.isSuccess()) {

			return result;
		}

		Long publishMemberId = personalFavorites.getPublishMemberId();

		String publishMemberName = personalFavorites.getPublishMemberName();

		result = checkMemberExist(publishMemberId, publishMemberName);

		if (!result.isSuccess()) {

			return result;
		}

		return ApiResult.newSuccessResult(true);

	}

	private ApiResult<Boolean> checkMemberExist(Long memberId, String memberName) {

		PersonalMember personalMember = new PersonalMember();

		personalMember.setMemberId(memberId);

		// personalMember.setMemberName(memberName);

		ApiResult<PersonalMember> result = personalMemberService.checkMember(personalMember);

		if (result.isSuccess()) {
			return ApiResult.newSuccessResult();
		} else {

			return ApiResult.newErrorResult(result.getErrorCode(), result.getErrorMsg(), false);
		}

	}

	private void fillParam(PersonalFavorites personalFavorites) {

		Long memberId = personalFavorites.getMemberId();

		Long publishMemberId = personalFavorites.getPublishMemberId();

		PersonalMember personalMember = personalMemberManager.get(memberId);

		if (personalMember != null) {

			personalFavorites.setMemberName(personalMember.getMemberName());
		}

		personalMember = personalMemberManager.get(publishMemberId);

		if (personalMember != null) {

			personalFavorites.setPublishMemberName(personalMember.getMemberName());
		}

	}

	private List<PersonalFavoritesItemContext> convertPersonalFavoritesItemContext(
			List<PersonalFavorites> personalFavoritesList) {

		List<PersonalFavoritesItemContext> personalFavoritesItemContextList = new ArrayList<PersonalFavoritesItemContext>();

		if (personalFavoritesList == null || personalFavoritesList.size() == 0) {

			return personalFavoritesItemContextList;
		}

		for (PersonalFavorites personalFavorites : personalFavoritesList) {
			ApiResult<ItemDO> itemDetailResult = itemReadService.getItemDetail(Long.valueOf(personalFavorites.getTargetCode()), false);

			PersonalFavoritesItemContext personalFavoritesItemContext = new PersonalFavoritesItemContext();
			
			//检查商品状态
			if (null != itemDetailResult && itemDetailResult.isSuccess() && null != itemDetailResult.getData()) {
				personalFavoritesItemContext.setItemStatus(itemDetailResult.getData().getStatus());;
			}

			personalFavoritesItemContext.setFavoritesId(personalFavorites.getFavoritesId());

			personalFavoritesItemContext.setMemberId(personalFavorites.getMemberId());

			personalFavoritesItemContext.setMemberName(personalFavorites.getMemberName());

			personalFavoritesItemContext.setPublishMemberId(personalFavorites.getPublishMemberId());

			personalFavoritesItemContext.setPublishMemberName(personalFavorites.getPublishMemberName());

			personalFavoritesItemContext.setTargetCode(personalFavorites.getTargetCode());

			personalFavoritesItemContext.setType(personalFavorites.getType());

			personalFavoritesItemContext.setPublishFileId(personalFavorites.getPublishFileId());

			personalFavoritesItemContext.setTimeAdd(personalFavorites.getTimeAdd());

			personalFavoritesItemContext.setTimeEdit(personalFavorites.getTimeEdit());

			personalFavoritesItemContext.setStatus(personalFavorites.getStatus());

			String itemId = personalFavorites.getTargetCode();

			// 设置商品细节
			ApiResult<ItemDO> api = itemReadService.getItemDetail(Long.valueOf(itemId), false);

			if (api.isSuccess() && api.getData() != null) {

				ItemDO item = api.getData();

				personalFavoritesItemContext.setItemName(item.getItemDesc());

				personalFavoritesItemContext.setItemPic(item.getPic().toString());

				personalFavoritesItemContext.setItemPrice(String.valueOf(item.getPrice()));

				personalFavoritesItemContext.setItemPriceCurno(item.getPriceCurno());
				;

				personalFavoritesItemContext.setItemCode(item.getItemCode());

				personalFavoritesItemContext.setBrandTitle(item.getBrandTitle());

				personalFavoritesItemContext.setStock(item.getStock());
				// 设置库存为同商品编码商品的总库存
				if (null != item.getItemCode()) {
					ItemQueryDO itemDoByItemCode = new ItemQueryDO();
					itemDoByItemCode.setItemCode(item.getItemCode());
					itemDoByItemCode.setPageSize(1000);
					Integer totalStock = 0;
					ApiResult<List<ItemDO>> itemListResult = itemReadService.queryItem(itemDoByItemCode);
					if (null != itemListResult && !CollectionUtils.isEmpty(itemListResult.getData())) {
						for (ItemDO itemSameCode : itemListResult.getData()) {
							totalStock += itemSameCode.getStock();
						}
						personalFavoritesItemContext.setStock(totalStock);
					}
				}

				// personalFavoritesItemContext.setItemPromotionPrice(String
				// .valueOf(item.getPromotionPrice()));

			}

			personalFavoritesItemContextList.add(personalFavoritesItemContext);

		}

		return personalFavoritesItemContextList;
	}

	private List<PersonalFeed> convertPersonalFeedList(List<PersonalFavorites> personalFavoritesList, Long memberId) {

		List<PersonalFeed> personalFeedList = new ArrayList<PersonalFeed>();

		if (personalFavoritesList == null || personalFavoritesList.size() == 0) {

			return personalFeedList;
		}

		for (PersonalFavorites personalFavorites : personalFavoritesList) {

			Long feedId = Long.valueOf(personalFavorites.getTargetCode());

			PersonalPublishFiles personalPublishFiles = new PersonalPublishFiles();

			personalPublishFiles.setPublishFileId(feedId);
			personalPublishFiles.setMemberId(memberId);

			ApiResult<ArrayList<PersonalFeed>> api = personalPublishFilesService.getFilesById(personalPublishFiles, 1);

			// 只有条件都满足了 才查出来 否则没有数据
			if (api.isSuccess() && api.getData() != null && api.getData().size() == 1) {

				List<PersonalFeed> list = api.getData();

				personalFeedList.add(list.get(0));

			}
		}

		return personalFeedList;

	}

	private boolean isNumeric(String str) {
		for (int i = 0; i < str.length(); i++) {
			System.out.println(str.charAt(i));
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	public void updateFeed(PersonalFavorites personalFavorites) {

		if (personalFavorites.getType().equals(AzerothConstants.PersonalFavtories.PersonalFavtoriesType.POST)) {

			PersonalPublishFiles personalPublishFiles = new PersonalPublishFiles();

			Integer count = queryFavoriteCnt(null, personalFavorites.getType(), personalFavorites.getTargetCode())
					.getData();

			personalPublishFiles.setFavoriteCount(count);

			Long publishFileId = Long.valueOf(personalFavorites.getTargetCode());

			personalPublishFiles.setPublishFileId(publishFileId);

			personalPublishFilesManager.update(personalPublishFiles);

		}

	}

	/**
	 * 换算成目标币种的价格
	 * 
	 * @param itemDO
	 * @param targetCurno
	 * @return
	 */
	public PersonalFavoritesItemContext exchangePrice(PersonalFavoritesItemContext personalFavoritesItemContext,
			String targetCurno) {
		if (StringUtils.isEmpty(targetCurno)) {
			targetCurno = "CNY";
		}

		if (targetCurno.equalsIgnoreCase(personalFavoritesItemContext.getItemPriceCurno())) {
			return personalFavoritesItemContext;
		}
		try {
			long price = Long.valueOf(personalFavoritesItemContext.getItemPrice());

			// 转化成人民币
			if (StringUtils.isNotEmpty(personalFavoritesItemContext.getItemPriceCurno())
					&& !"CNY".equalsIgnoreCase(personalFavoritesItemContext.getItemPriceCurno())) {
				price = configExchangeRateManager.transferToCNY(personalFavoritesItemContext.getItemPriceCurno(),
						price);
			}
			// 转化成目标货币
			if (StringUtils.isNotEmpty(targetCurno) && !"CNY".equalsIgnoreCase(targetCurno)) {
				price = configExchangeRateManager.transferFromCNY(targetCurno, price);
			}
			personalFavoritesItemContext.setItemPriceCurno(targetCurno);
			personalFavoritesItemContext.setItemPrice(String.valueOf(price));
		} catch (Exception e) {
			log.error("invalid price:" + personalFavoritesItemContext.getItemPrice());
		}
		return personalFavoritesItemContext;
	}
}
